﻿using System;
using System.Windows.Forms;

namespace Business.Controls
{
    public class CommandCancel : CommandBase
    {
        
        
    
        public CommandCancel()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            
            this.SuspendLayout();
            // 
            // CommandCancel
            // 
            this.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.Atalho = System.Windows.Forms.Keys.Escape;
            this.Text = "Cancelar(Esc)";
            this.Click += new System.EventHandler(this.CommandDelete_Click);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CommandInsert_KeyDown);
            this.ResumeLayout(false);

        }

        private void CommandInsert_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            
            if (e.KeyData == Atalho)
            {
                Cancel();
            }
        }

        private void Cancel()
        {
            try
            {
                if (!Enabled)
                {
                    return;
                }
                if (Form.Form.DialogResult != DialogResult.Cancel)
                {
                    try
                    {
                        Form.Reload();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Erro ao recarregar objeto.\n" +
                            ex.Message);
                    }
                    Form.Cancel();
                }
                
            }
            catch (Exception ex)
            {
                ExibeErro("Erro ao cancelar edição de item.\n");
            }
        }

        private void CommandDelete_Click(object sender, System.EventArgs e)
        {
            Cancel();
        }

        
    }
}
